/*
 * ProcessPreviewPanel.java
 *
 * Created on 07.11.2008., 15.18
 */
package rs.fon.rapidminer.gui.extended;

import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import rs.fon.rapidminer.process.Parameter;
import rs.fon.rapidminer.process.Step;
import rs.fon.rapidminer.process.StepData;

/**
 *This class representing extending JPanel class and contains JTree component
 * which represents Process.
 * 
 * @author Nikola Nikolic
 */
public class ProcessPreviewPanel extends javax.swing.JPanel {

	/**
     * 
     */
	private static final long serialVersionUID = 1032090920935913360L;
	private final ImageIcon PROCESS_ICON = new ImageIcon("");
	private final ImageIcon STEP_ICON = new ImageIcon("");
	private final ImageIcon CLASS_ICON = new ImageIcon("");
	private final ImageIcon PARAMETER_ICON = new ImageIcon("");
	private final ImageIcon VALUE_ICON = new ImageIcon("");

	/** Creates new form ProcessPreviewPanel */
	public ProcessPreviewPanel() {
		initComponents();
	}

	/**
	 *This method creates JTree component form given Process
	 * 
	 *@param process
	 *            {@link Process}
	 */
	public void refreshTreePreview(rs.fon.rapidminer.process.Process process) {

		DefaultMutableTreeNode rootProcess = new DefaultMutableTreeNode(
				new IconData(PROCESS_ICON, process.getName()));

		List<Step> processSteps = process.getProcessSteps();

		if (processSteps != null && processSteps.size() > 0) {
			for (Step step : processSteps) {

				DefaultMutableTreeNode stepNode = new DefaultMutableTreeNode(
						new IconData(STEP_ICON, step.getName()));

				if (step.isMultiple()) {
					List<StepData> multipleStepData = step
							.getMultipleStepData();
					if (multipleStepData != null && multipleStepData.size() > 0) {

						for (StepData stepData : multipleStepData) {

							DefaultMutableTreeNode stepDataNode = null;
							try {
								stepDataNode = new DefaultMutableTreeNode(
										new IconData(
												STEP_ICON,
												Class
														.forName(
																stepData
																		.getNameOfImplementationClass())
														.getSimpleName()));

								List<Parameter> params = stepData
										.getListOfParameters();

								if (params != null && params.size() > 0) {
									for (Parameter parameter : params) {
										DefaultMutableTreeNode paramNode = new DefaultMutableTreeNode(
												new IconData(
														PARAMETER_ICON,
														parameter
																.getNameOfParameter()));
										DefaultMutableTreeNode valueNode = new DefaultMutableTreeNode(
												new IconData(
														VALUE_ICON,
														parameter
																.getXenteredValue()));
										paramNode.add(valueNode);
										stepDataNode.add(paramNode);

									}
								}
							} catch (ClassNotFoundException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							stepNode.add(stepDataNode);
						}
					}

				} else {
					if (step.getStepData() != null) {
						DefaultMutableTreeNode stepDataNode = null;
						try {
							stepDataNode = new DefaultMutableTreeNode(
									new IconData(
											STEP_ICON,
											Class
													.forName(
															step
																	.getStepData()
																	.getNameOfImplementationClass())
													.getSimpleName()));

							List<Parameter> params = step.getStepData()
									.getListOfParameters();
							if (params != null && params.size() > 0) {
								for (Parameter parameter : params) {
									DefaultMutableTreeNode paramNode = new DefaultMutableTreeNode(
											new IconData(
													PARAMETER_ICON,
													parameter
															.getNameOfParameter()));
									DefaultMutableTreeNode valueNode = new DefaultMutableTreeNode(
											new IconData(VALUE_ICON, parameter
													.getXenteredValue()));
									paramNode.add(valueNode);
									stepDataNode.add(paramNode);
								}
							}
						} catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						stepNode.add(stepDataNode);
					}
				}
				rootProcess.add(stepNode);

			}

		}
		DefaultTreeModel model = new DefaultTreeModel(rootProcess);

		jTreeProcessPreview.setModel(model);

		try {
			// Expand all the tree rows...
			int rc = 0;
			do {
				rc = this.getTree().getRowCount();
				for (int x = rc; x >= 0; x--) {
					this.getTree().expandRow(x);
				}
			} while (rc != this.getTree().getRowCount());
		} catch (Exception e) {
			// Throw when new process selected. Cause not clear yet.
		}

	}

	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jTreeProcessPreview = new javax.swing.JTree();

		jScrollPane1.setViewportView(jTreeProcessPreview);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220,
				Short.MAX_VALUE));

	}// </editor-fold>

	private JTree getTree() {
		return jTreeProcessPreview;
	}

	// Variables declaration - do not modify
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTree jTreeProcessPreview;
	// End of variables declaration
}
